// pages/forget-password/index.js
const { $Message } = require('../../dist/base/index');
const handler = require("../../utils/function.js");
Page({

  /**
   * 页面的初始数据
   */
  data: {
    sendtxt: "获取验证码",
    phone: null,
    phoneTest: false,
    verificationcode: '',
    verificationcodeTest: false,
    gettingCode: true,
    password: '',
    passwordTest: false,
    user_type:''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({ user_type:options.type})
 
  },
  // phoneinput 获取电话号码
  phoneinput: function (event) {
    var that = this
    that.setData({
      phone: event.detail.value
    })
    this.phoneTest(event.detail.value)
  },
  // phoneTest 电话号码验证
  phoneTest: function (params) {
    if (params == '' || !/^1\d{10}$/.test(params)) {
      wx.showToast({
        title: '请输入正确的电话号码格式',
        icon: "none"
      })
      this.setData({
        phoneTest: false,
      });
      return;
    } else {
      this.setData({
        phoneTest: true,
      });
    }
  },
  // 发送验证码
  sendcode: function () {
    var that = this;
    this.phoneTest(this.data.phone)

    handler.requestData("sendSMS", { phone: that.data.phone }, function (res) {
      if (res.success == true) {
        $Message({
          content: '验证码发送成功,有效期2小时',
          type: 'success'
        });
      } else {
        $Message({
          content: res.message,
          type: 'error'
        });
      }
    });
  },

  //倒计时 倒数
  countDown: function () {
    var that = this;
    that.data.gettingCode = false;
    var seconds = 59;
    var countTimer = setInterval(function () {
      that.setData({
        sendtxt: seconds + "s"
      })
      seconds--;
      if (seconds <= 0) {
        countTimer = clearInterval(countTimer);
        that.setData({
          sendtxt: '获取验证码'
        })
        that.data.gettingCode = true;
      }
    }, 1000);
  },
  verificationinput: function (e) {
    this.setData({
      verificationcode: e.detail.value,
    });
    this.verificationTest(e.detail.value);
  },
  verificationTest: function (params) {
    if (params == null) {
      wx.showToast({
        title: '请输入验证码',
        icon: "none"
      })
      return;
    } else {
      this.setData({
        verificationcodeTest: true,
      });
    }
  },
  // passwordinput 获取密码
  passwordinput: function (e) {
    console.log(e.detail.value);
    this.setData({
      password: e.detail.value
    });
    this.passwordTest(e.detail.value);
  },
  // passwordTest 密码验证
  passwordTest: function (params) {
    if (params == '' || !/^\d{6}$/.test(params)) {
      wx.showToast({
        title: '请输入正确的密码格式',
        icon: "none"
      })
      return;
    } else {
      this.setData({
        passwordTest: true,
      });
    }
  },
  // getPhoneLogin 登录的电话号码
  getPhoneLogin: function () {
    const {
      sendtxt,
      phone,
      phoneTest,
      verificationcode,
      verificationcodeTest,
      password,
      passwordTest,
     } = this.data;
    var submitJson = {
      phone:phone,
      code: verificationcode,
      password:password,
      user_type: wx.getStorageSync('temp_user_type')
    };
    // console.log(wx.getStorageSync('temp_user_type'));
    var that = this;
    if (verificationcodeTest && passwordTest && phoneTest) {
      handler.requestData("modifyPassword", submitJson, function (res) {
        if (res.success == true) {
          $Message({
            content: '修改成功',
            type: 'success'
          });
          // wx.setStorageSync("user_phone", this.data.phone)
          handler.intoPageHandler("../login/index?type=" + that.data.user_type);
        } else {
          $Message({
            content: res.message,
            type: 'error'
          });
        }
      });
      
      // wx.setStorageSync("user_phone", this.data.phone)
      // handler.intoPageHandler("../login/index");
    } else {
      $Message({
        content: '修改失败',
        type: 'error'
      });
    }

    
 
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})